export default class SocketService {
  static instance: SocketService | null = null;
  static get Instance() {
    if (!this.instance) this.instance = new SocketService();
    return this.instance;
  }
  ws: WebSocket | null = null;
  connect() {
    if (!window.WebSocket) {
      return console.log("您的浏览器不支持websocket");
    }
    this.ws = new WebSocket("ws://localhost:9998");
    this.ws.onopen = () => {
      console.log("websocket连接成功");
      this.ws?.send("123");
    };
    this.ws.onmessage = (msg) => {
      console.log("websocket接收消息: ", msg);
    };
    this.ws.onclose = () => {
      console.log("websocket断开连接");
    };
  }
  send(msg: string) {
    if (this.ws) this.ws.send(msg);
  }
}
